package xml.resource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.dbutils.DbUtils;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import xml.model.Promotion;
import xml.validator.LongRangeValidator;

/**
 * Resource permettant d'obtenir l'URI d'une promotion
 * Méthodes: GET
 * @author marya
 */
public class PromotionHREFResource extends AbstractResource {

    private Promotion promotion;

    public PromotionHREFResource() {
        super();
    }

    @Override
    protected void doInit() {
        Long idPromotion;
        final String sqlPromotion = "SELECT * FROM groupe WHERE id_groupe = ";
        String idPromotionAttribute = getRequest().getAttributes().get("idPromotion").toString();
        System.out.println("INIT: " + idPromotionAttribute);
        LongRangeValidator longRangeValidator = new LongRangeValidator(1l, null);
        Connection connexion = null;
        //Validation de l'id de la promotion
        if (longRangeValidator.isValid(idPromotionAttribute)) {
            idPromotion = Long.parseLong(idPromotionAttribute);
        } else {
            errors.add(new ResourceError("invalidPromotion"));
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
        }
        try {
            connexion = database.getNewConnection();
            Statement statement = connexion.createStatement();

            ResultSet rs = statement.executeQuery(sqlPromotion + idPromotion);
            if (!rs.next()) {
                setExisting(false);
            } else {
                promotion = new Promotion(rs.getInt("id_groupe"), rs.getString("nom"), null);
                if (rs.getString("url") == null) {
                    throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Bad request to attempt groups");
                } else {
                    promotion.setUri(getRootRef().getHostIdentifier() + "/promotions/" + promotion.getIdGroupe());
                }
            }
            connexion.close();

        } catch (SQLException e) {
            DbUtils.closeQuietly(connexion);
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
        }
    }

    @Get
    public Representation doGet() throws Exception {
        Representation response = null;
        final String sqlPromotion = "SELECT url FROM groupe WHERE id_groupe = " + promotion.getIdGroupe();
        Connection connexion = null;
        ResultSet rs = null;
        try {
            connexion = database.getNewConnection();
            rs = connexion.createStatement().executeQuery(sqlPromotion);
            rs.next();
            Promotion result = new Promotion(promotion.getIdGroupe(), promotion.getNom(), null);
            result.setUri(rs.getString("url"));
            response = result.toXML();
            connexion.close();
        } catch (SQLException e) {
            DbUtils.closeQuietly(connexion);
            DbUtils.closeQuietly(rs);
            throw e;
        }

        return response;
    }
}
